package com.dreamlin.lindownload.entity;

import com.dreamlin.lindownload.LinDownload;

import java.util.List;

import io.realm.Realm;
import io.realm.RealmObject;
import io.realm.RealmResults;
import io.realm.annotations.PrimaryKey;

import static com.dreamlin.lindownload.LinDownload.DEFAULT_AUTO_RETRY;
import static com.dreamlin.lindownload.LinDownload.DEFAULT_INTERVAL;
import static com.dreamlin.lindownload.LinDownload.DEFAULT_THREAD_NUM;

/**
 * <p> Title: LinTaskBean </p>
 * <p> Description: 记录下载任务信息</p>
 *
 * @author: dreamlin
 * @date: 2020/5/5
 * @version: V1.0.0
 * Created by dreamlin on 2020/5/5.
 */
public class LinTaskBean extends RealmObject {

    private String fileName;
    @PrimaryKey
    private String url;
    private String savePath;
    private long length;
    private long callInterval;
    private int autoRetry;
    private int threadNum;
    private int state = LinDownload.STATUS_NONE;

    private LinTaskBean(Builder builder) {
        this.fileName = builder.fileName;
        this.url = builder.url;
        this.savePath = builder.savePath;
        this.length = builder.length;
        this.callInterval = builder.callInterval;
        this.autoRetry = builder.autoRetry;
        this.threadNum = builder.threadNum;
    }

    public LinTaskBean() {
    }

    public LinTaskBean(String url) {
        setUrl(url);
    }

    public String getFileName() {
        if (fileName == null || fileName.isEmpty()) {
            if (url != null && !url.isEmpty()) {
                String subParams = url;
                if (url.contains("?")) {
                    subParams = subParams.substring(0, url.lastIndexOf("?"));
                }
                fileName = subParams.substring(url.lastIndexOf('/') + 1);
            }
        } else {
            if (!fileName.contains(".")) {
                String suffix = url.substring(url.lastIndexOf('.'));
                fileName = fileName.concat(suffix);
            }
        }
        return fileName;
    }

    public String getUrl() {
        return url;
    }

    public String getSavePath() {
        return savePath;
    }

    public long getLength() {
        return length;
    }

    public void setLength(long length) {
        this.length = length;
    }

    public long getCallInterval() {
        return callInterval;
    }

    public int getAutoRetry() {
        return autoRetry;
    }

    public int getThreadNum() {
        return threadNum;
    }

    public int getState() {
        return state;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public void setUrl(String url) {
        this.url = url;
        if (fileName != null) {
            if (!fileName.contains(".")) {
                String subParams = url;
                if (url.contains("?")) {
                    subParams = subParams.substring(0, url.lastIndexOf("?"));
                }
                String suffix = subParams.substring(subParams.lastIndexOf('.'));
                fileName = fileName.concat(suffix);
            }
        }
    }

    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }

    public void setCallInterval(long callInterval) {
        this.callInterval = callInterval;
    }

    public void setAutoRetry(int autoRetry) {
        this.autoRetry = autoRetry;
    }

    public void setThreadNum(int threadNum) {
        this.threadNum = threadNum;
    }

    public void setState(int state) {
        this.state = state;
    }

    public List<LinThreadBean> getThreadInfoList() {
        //TODO:查询数据库里的线程信息

        Realm realm = Realm.getDefaultInstance();
        try {
            RealmResults<LinThreadBean> threads = realm.where(LinThreadBean.class)
                    .equalTo("url", LinTaskBean.this.url)
                    .findAll();
            if (threads != null && !threads.isEmpty()) {
                return realm.copyFromRealm(threads);
            }
        } finally {
            realm.close();
        }
        return null;
    }

    public void update() {
        Realm realm = Realm.getDefaultInstance();
        try {
            realm.beginTransaction();
            realm.copyToRealmOrUpdate(this);
            realm.commitTransaction();
        } finally {
            realm.close();
        }
    }

    public static class Builder {
        private String fileName;
        private String url;
        private String savePath;
        private long length = -1;
        private long callInterval = DEFAULT_INTERVAL;
        private int autoRetry = DEFAULT_AUTO_RETRY;
        private int threadNum = DEFAULT_THREAD_NUM;

        public Builder setFileName(String fileName) {
            this.fileName = fileName;
            return this;
        }

        public Builder setUrl(String url) {
            this.url = url;
            return this;
        }

        public Builder setSavePath(String savePath) {
            this.savePath = savePath;
            return this;
        }

        public Builder setLength(long length) {
            this.length = length;
            return this;
        }

        public Builder setCallInterval(long callInterval) {
            this.callInterval = callInterval;
            return this;
        }

        public Builder setAutoRetry(int autoRetry) {
            this.autoRetry = autoRetry;
            return this;
        }

        public Builder setThreadNum(int threadNum) {
            this.threadNum = threadNum;
            return this;
        }

        public LinTaskBean build() {
            if (fileName == null || fileName.isEmpty()) {
                fileName = url.substring(url.lastIndexOf('/') + 1);
            }
            return new LinTaskBean(this);
        }
    }

}
